#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Bootloader_ReadDataById_0x22_test.py
@Time    :   2024/09/03 13:31:28
@Author  :   yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
import Set_up
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

readDataByID_NRC_Boot = [   ([0x22], [0x7F, 0x22, 0x13]),  # NRC13
                            ([0x22, 0xF1], [0x7F, 0x22, 0x13]),
                            ([0x22, 0x12], [0x7F, 0x22, 0x13]),
                            ([0x22, 0xF1, 0x84, 0x01], [0x7F, 0x22, 0x13]),
                            # SYSTEM DID
                            ([0x22, 0xF1, 0x90], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xF1, 0x87], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xF1, 0x8A], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xF1, 0x91], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xF1, 0x95], [0x7F, 0x22, 0x31]),
                            # ECU DID
                            ([0x22, 0xF1, 0x8B], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xF1, 0x8C], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xF1, 0x99], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x60], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x61], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x62], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x63], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x64], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x65], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x66], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x68], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x69], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x6A], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x6B], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x12, 0x6C], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xFE, 0x00], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xFE, 0x01], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xFE, 0x02], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xFE, 0x03], [0x7F, 0x22, 0x31]),
                            ([0x22, 0xFE, 0x04], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x10, 0x00], [0x7F, 0x22, 0x31]),
                            ([0x22, 0x10, 0x01], [0x7F, 0x22, 0x31]),
                        ]


readDataByID_normal_Boot = [([0x22, 0xF1, 0x83], [0x62, 0xF1, 0x83] + Set_up.BootloaderSoftwareVersionNumber),  # Bootloader软件版本号
                            ([0x22, 0xF1, 0x84], [0x62, 0xF1, 0x84] + Set_up.ApplicationSoftwareFingerprint)]  # 应用软件指纹信息]  # Bootloader软件版本号]


class Test_ReadDataById_Phy_Boot(object):

    ids = ['Request:{0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in readDataByID_normal_Boot]
    @allure.story('Read data by identifier test')
    @allure.description('Test DID of 0x22 in programming session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', readDataByID_normal_Boot, ids=ids)
    def test_ReadDataById_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        if req == [0x22, 0xF1, 0x84]:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E F1 84" + Set_up.ApplicationSoftwareFingerprint1, '6E F1 84', 2, iStep, True)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)

    ids = ['Request:{0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in readDataByID_NRC_Boot]
    @allure.description('Test NRC of 0x22 in programming session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', readDataByID_NRC_Boot, ids=ids)
    def test_ReadDataById_NRC_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)